﻿namespace ConcurrentBoo.Executors

import System
import System.Threading
import System.Windows.Forms as SWF

class MessageLoopExecutor(ThreadedExecutor):
"""
Executes any actions in a thread with its own message loop. Good
for components with thread affinity that also need a message loop
(STA COM components, for instance).
"""
	_apartmentState as ApartmentState
	
	def constructor(apartmentState as ApartmentState):
		_apartmentState = apartmentState
		
	def constructor():
		self(ApartmentState.STA)
		
	override protected def CreateThread():
		thread = super()
		thread.SetApartmentState(_apartmentState)
		return thread
	
	override protected def ExecutionLoop():
		while true:		
			message = null
			if _channel.TryTake(0, message):	
				if not ProcessMessage(message):
					break
			SWF.Application.DoEvents()
